package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.menu.api.SysMenuService;
import com.nidu.demo.menu.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 菜单管理 Controller
 */
@Tag(name = "管理后台 - 菜单")
@RestController
@RequestMapping("/system/menu")
@Validated
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService service;

    @Operation(summary = "创建菜单", description = "创建新的菜单")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:menu:create')")
    public SingleResponse<MenuCO> addMenu(@RequestBody MenuAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新菜单", description = "更新菜单信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:menu:update')")
    public SingleResponse<MenuCO> updateMenu(@PathVariable Long id, @RequestBody MenuUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除菜单", description = "根据 ID 删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    public Response deleteMenu(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除菜单", description = "根据 ID 列表删除多个菜单")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    public Response deleteMenus(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取菜单详情", description = "根据 ID 获取菜单详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public SingleResponse<MenuCO> getMenuById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询菜单列表", description = "获取所有菜单信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public MultiResponse<MenuCO> listMenus(MenuListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "查询菜单树", description = "分页查询菜单树信息")
    @GetMapping("/tree")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public MultiResponse<MenuTreeCO> treeMenus(MenuTreeQry qry) {
        return service.tree(qry);
    }

    @Operation(summary = "分页查询菜单树", description = "分页查询菜单树信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public PageResponse<MenuTreeCO> pageMenus(MenuPageQry qry) {
        return service.page(qry);
    }

    @Operation(summary = "更新菜单状态", description = "更新菜单的启用/禁用状态")
    @PutMapping("/{id}/status")
    @PreAuthorize("@ss.hasPermission('system:menu:update')")
    public Response updateStatus(@PathVariable Long id, @RequestParam StatusEnum status) {
        return service.updateStatus(id, status);
    }
}
